package com.lxj.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lxj.common.CodeMsg;
import com.lxj.common.Result;
import com.lxj.domain.ActiveUser;
import com.lxj.domain.BusCar;
import com.lxj.domain.BusCustomer;
import com.lxj.domain.BusRent;
import com.lxj.dto.BusCarDto;
import com.lxj.mapper.BusCarMapper;
import com.lxj.mapper.BusCustomerMapper;
import com.lxj.mapper.BusRentMapper;
import com.lxj.service.CarService;
import com.lxj.vo.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author lxj
 * @date 2021/6/21 9:47
 * @Description:
 */
@Service
public class CarServiceImpl implements CarService {

    @Autowired
    private BusCarMapper carMapper;

    @Autowired
    private BusRentMapper rentMapper;

    @Autowired
    private BusCustomerMapper busCustomerMapper;

    /**
     * 分页查询
     * @param dto
     * @return
     */
    @Override
    public PageInfo<BusCar> findByPage(BusCarDto dto) {
        Page<BusCar> page = PageHelper.startPage(dto.getPage(), dto.getLimit());
        List<BusCar> cars = carMapper.selectAll(dto);
        return new PageInfo<>(page.getTotal(),cars);
    }

    /**
     * 新增
     * @param dto
     * @return
     */
    @Override
    @Transactional
    public Result add(BusCarDto dto) {
        BusCar busCar = new BusCar();
        BeanUtils.copyProperties(dto,busCar);
        busCar.setCreateTime(new Date());
        busCar.setIsRent(0);
        busCar.setVersion(1);
        carMapper.insert(busCar);
        return new Result();
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @Override
    @Transactional
    public Result batchDelete(Integer[] ids) {
        carMapper.batchDelete(ids);
        return new Result();
    }

    /**
     * 修改
     * @param dto
     * @return
     */
    @Override
    @Transactional
    public Result updateById(BusCarDto dto) {
        BusCar busCar = new BusCar();
        BeanUtils.copyProperties(dto,busCar);
        carMapper.updateByPrimaryKeySelective(busCar);
        return new Result();
    }

    /**
     * 租车
     * @param carId
     * @param idCard
     * @param startTime
     * @param endTime
     * @return
     */
    @Override
    @Transactional
    public Result rentCar(Integer carId, String idCard, String startTime, String endTime) {
        //校验车辆
        Result resultCar = validateCarForRent(carId);
        BusCar car = (BusCar) resultCar.getData();
        //校验客户
        Result resultCustom = validateCustomerForRent(idCard);
        BusCustomer customer = (BusCustomer) resultCustom.getData();
        //写租车表
        int insert = createCarRent(startTime, endTime, car, customer);
        //修改车辆的状态
        if (insert > 0) {
            // 修状态为已经借出
            car.setIsRent(1);
            carMapper.updateByPrimaryKey(car);
        }
        return new Result();
    }

    /**
     * 修改车辆的状态
     * @param startTime
     * @param endTime
     * @param car
     * @param customer
     * @return
     */
    private int createCarRent(String startTime, String endTime, BusCar car, BusCustomer customer) {
        BusRent busRent = new BusRent();
        BeanUtils.copyProperties(car,busRent);
        BeanUtils.copyProperties(customer,busRent);
        // 填充默认的数据
        busRent.setBeginTime(startTime);
        busRent.setEndTime(endTime);
        busRent.setFlag(2);
        busRent.setCreateTime(new Date());
        busRent.setUpdateTime(new Date());
        // 设置业务员的id
        busRent.setUserId(getCurrentUser());
        // 让租车表的id自己增长
        busRent.setId(null);
        return rentMapper.insert(busRent);
    }

    /**
     * 获取业务员的id
     * @return
     */
    private Integer getCurrentUser() {
        ActiveUser principal = (ActiveUser)SecurityUtils.getSubject().getPrincipal();
        return principal.getUser().getId();
    }

    /**
     * 租车时校验客户的身份
     * @param idCard
     * @return
     */
    private Result validateCustomerForRent(String idCard) {
        BusCustomer busCustomer = busCustomerMapper.selectByIdCard(idCard);
        if (busCustomer == null) {
            return new Result(CodeMsg.CUSTOM_ID_CARD_ERROR);
        }
        return new Result(busCustomer);
    }

    /**
     * 租车时校验车辆的数据
     * @param carId
     * @return
     */
    private Result validateCarForRent(Integer carId) {
        BusCar car = carMapper.selectByPrimaryKey(carId);
        if (car == null) {
            return new Result(CodeMsg.CAR_ID_ERROR);
        }
        if (car.getIsRent() != null && car.getIsRent() == 1) {
            return new Result(CodeMsg.CAR_IS_RENT_ERROR);
        }
        return new Result(car);
    }
}
